package com.project.service.shopService.impl;

import com.project.bean.GoodsBean;
import com.project.bean.ShopBean;
import com.project.mapper.goodsMapper.IGoodsMapper;
import com.project.mapper.goodsOrderMapper.IGoodsOrderMapper;
import com.project.service.goodsOrderService.IGoodsOrderService;
import com.project.service.goodsService.IGoodsService;
import com.project.service.shopService.IShopService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Map;

@Service
@Transactional
public class ShopServicelmpl implements IShopService {
    @Autowired
    private IGoodsMapper goodsMapper;
    @Autowired
    private IGoodsOrderMapper goodsOrderMapper;

    @Override
    public void add(GoodsBean goods, Map<Integer, ShopBean> shopMap, int num) {
        if(shopMap.containsKey(goods.getId()) == false){//商品不存在，新加元素
            //创建购物车对象
            ShopBean ShopBean = new ShopBean();
            //设置采购数量
            ShopBean.setNum(num);
            //添加新商品
            ShopBean.setGoodsBean(goods);
            shopMap.put(goods.getId(), ShopBean);
        }else {
            //根据ID找到对应的购物车对象
            ShopBean ShopBean = shopMap.get(goods.getId());
            ShopBean.setNum(ShopBean.getNum()+num);
        }
    }

    @Override
    public void del(int goodsId, Map<Integer, ShopBean> shopMap) {
        shopMap.remove(goodsId);
    }

    @Override
    public void update(int goodsId, Map<Integer, ShopBean> shopMap, int number) {
        ShopBean ShopBean = shopMap.get(goodsId);
        ShopBean.setNum(number);
    }
}
